<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Users;
use App\Models\Userinfo;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function index(Request $request)
    {
        //
        $title = '用户列表';
        // $data = Users::get();
        // dd($data);
        //获取所有数据
        $count = $request -> input('count',5);
        $search = $request -> input('search','');
        $request = $request -> all();
        
        $data = Users::where('uname','like','%'.$search.'%')->paginate($count);
       
        
        // dd($data);
        return view('admin.users.index',compact('data','title','request','count','search'));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

         // 1 查询出当前分类下的所有文章
        $users = Userinfo::where('uf_uid',$id)->get();
//        定义一个空字符串来接收每行的文章记录
        $str =' <center><table style="text-algin:center" border="0 solid #ccc" width="605px" height="90px"><tr><th class="tc">ID</th><th>真实姓名</th><th>性别</th><th>省份</th><th>市区</th> <th>生日</th><th>会员等级</th><th>积分</th></tr>';

        foreach($users as $k=>$v){
          $str.= "<tr >
               <th >{$v->ufid}</th>
               <th>{$v->ufname}</th>
               <th>{$v->ufsex}</th>
               <th>{$v->ufsheng}</th>
               <th>{$v->ufshi}</th>
               <th>{$v->ufbirthday}</th>
               <th>{$v->uf_vid}</th>
               <th>{$v->uf_score}</th>
              </tr>";
        }
        $str.= '</table>';
        // 2 将查询后的结果变成一个字符串格式的表格

        return $str;

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //

        $title = '用户状态修改';
        $data = Users::find($id);
        return view('admin.users.edit',compact('title','data'));

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //

        $data = $request->except('_token','_method');
        $data['uid'] = $id;
        // dd($data);
        $use = Users::find($id);
        $use->ustatus = $data['ustatus'];
        $re = $use->save();
        if($re){
            return redirect('/admin/users')->with('success','修改成功');
        }else{
            return back()->withInput('error','修改失败');
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
